﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Exercitation.BLL;
using Yipusoft.Exercitation.Model;
using System.Drawing;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;

public partial class Combat_ScoreView : System.Web.UI.Page
{
    string id = "55F5139F-489A-487D-8D55-FF6E555C7FDA";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            Bind();
    }

    private string GetStation()
    {
        string station = string.Empty;
        List<CodeItemInfo> itemList = Yipusoft.Common.BLL.CodeItem.GetList("ItemID IN (Select StationID From TaskSoldiers Where SoldierID='" + Request["SoldierID"] + "')");
       
        foreach (CodeItemInfo code in itemList)
        {
            station += code.ItemName + "、";
        }

        return station;
    }

    private void Bind()
    {
        TableRow trw;
        TableCell cel;
        Yipusoft.SoldierArchives.BLL.Soldier soldier = new Yipusoft.SoldierArchives.BLL.Soldier();
        Yipusoft.SoldierArchives.Model.SoldierArchiveInfo soldierInfo= soldier.GetSoldierInfoByID(Request["SoldierID"]);
        lblTitle.Text = "【" + soldierInfo.SoldierName + "】" + Request["year"] + "年" + Request["month"] + "月" + "训练成绩";
        trw = new TableRow();
        cel = new TableCell();
        System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();
        image.ImageUrl = "../Images/NoImage.gif";
        image.Width = 80;
        image.Height = 120;
        cel.Controls.Add(image);
        cel.RowSpan = 3;
        trw.Cells.Add(cel);
        cel = new TableCell();
        cel.Text = "士兵名称";
        trw.Cells.Add(cel);
        cel = new TableCell();
        cel.ColumnSpan = 3;
        cel.Text = soldierInfo.SoldierName;
        trw.Cells.Add(cel);
        tb.Rows.Add(trw);
        trw = new TableRow();

        cel = new TableCell();
        cel.Text = "士兵警衔";
        trw.Cells.Add(cel);
        cel = new TableCell();
        cel.Text = CodeItem.GetCodeItem(Request["RankID"]).ItemName;
        cel.ColumnSpan = 3;
        trw.Cells.Add(cel);
        tb.Rows.Add(trw);

        trw = new TableRow();
        cel = new TableCell();
        cel.Text = "士兵岗位";
        trw.Cells.Add(cel);

        cel = new TableCell();
        cel.Text = GetStation();
        cel.ColumnSpan = 3;
        trw.Cells.Add(cel);
        tb.Rows.Add(trw);

        List<ItemSkillInfo> itemList = ItemSkill.GetList(" ID IN(Select ParentID From ItemSkill Where ID IN( Select ParentID From ItemSkill Where SkillID IN (Select SkillID From TaskSubject Where TaskItemID IN(Select ID From TaskItems Where TaskID='" + Request["TaskID"] + "' And RankID='" + Request["RankID"] + "' And StationID IN(Select StationID From TaskSoldiers Where TaskID='" + Request["TaskID"] + "' And SoldierID='" + Request["SoldierID"] + "')))))");
      
       foreach (ItemSkillInfo itemInfo in itemList)
       {
           trw = new TableRow();
           cel = new TableCell();
           cel.Text = itemInfo.AlisSkillName;
           trw.Cells.Add(cel);
           int rowCount= BindChild(trw, itemInfo.ID.ToString());
           if(rowCount>0)
            cel.RowSpan = rowCount;
       }
    }

    private int BindChild(TableRow trw,string parentID)
    {
        int count = 0;
        TableCell cel;
        List<ItemSkillInfo> itemList = ItemSkill.GetList(" ParentID='"+parentID+"' And  ID IN( Select ParentID From ItemSkill Where SkillID IN (Select SkillID From TaskSubject Where TaskItemID IN(Select ID From TaskItems Where TaskID='" + Request["TaskID"] + "' And RankID='" + Request["RankID"] + "' And StationID IN(Select StationID From TaskSoldiers Where TaskID='" + Request["TaskID"] + "' And SoldierID='" + Request["SoldierID"] + "'))))");
        foreach (ItemSkillInfo itemInfo in itemList)
        {

            cel = new TableCell();
            cel.Text = itemInfo.AlisSkillName;            
            trw.Cells.Add(cel);
           
            int rowCount = BindChild2(trw, itemInfo.ID.ToString());
            count += rowCount;
            if (rowCount > 0)
                cel.RowSpan = rowCount;

            trw = new TableRow();
        }
        return count;
    }

    private int BindChild2(TableRow trw, string parentID)
    {        
        List<ItemSkillInfo> itemList = ItemSkill.GetList(" ParentID='" + parentID + "' And SkillID IN (Select SkillID From TaskSubject Where TaskItemID IN(Select ID From TaskItems Where TaskID='"+Request["TaskID"]+"' And RankID='"+Request["RankID"]+"' And StationID IN(Select StationID From TaskSoldiers Where TaskID='"+Request["TaskID"]+"' And SoldierID='"+Request["SoldierID"]+"')))");
        int count = 0;
        TableCell cel;
        foreach (ItemSkillInfo itemInfo in itemList)
        {
            
            cel = new TableCell();
            cel.Text = itemInfo.AlisSkillName;
            trw.Cells.Add(cel);
            
            TaskScoreInfo scoreInfo= TaskScore.GetList(" TaskID='" + Request["TaskID"] + "' And SoldierID='" + Request["SoldierID"] + "' And SkillID='" + itemInfo.SkillID + "'").FirstOrDefault();
            string result = string.Empty;
            string Pass=string.Empty;
            if ((scoreInfo != null) && (!string.IsNullOrEmpty(scoreInfo.Score.Trim())))
            {
                TaskSkillInfo skillInfo = TaskSkill.GetTaskSkill(itemInfo.SkillID);
                if (skillInfo.SkillItemID.Equals("2"))
                    result = DateTime.Parse(scoreInfo.Score).ToString("HH时mm分ss秒");
                else
                    result = scoreInfo.Score;


                switch (skillInfo.SkillItemID)
                {
                    case "1":
                        Pass = scoreInfo.Score.Equals("合格") ? "合格" : "不合格";
                        break;

                    case "2":
                        DateTime dt = DateTime.Parse(scoreInfo.Score);
                        DateTime dt2 = DateTime.Parse(skillInfo.SkillStandard);
                        if (dt.Hour <= dt2.Hour && dt.Minute <= dt2.Minute && dt.Second <= dt2.Second)
                            Pass = (dt.Hour + dt.Minute + dt.Second) > 0 ? "合格" : "不合格";
                        else
                            Pass = "不合格";
                        break;
                    case "3":
                        Pass = int.Parse(scoreInfo.Score) >= int.Parse(skillInfo.SkillStandard) ? "合格" : "不合格";
                        break;

                }
                // 训练成绩
                cel = new TableCell();
                cel.Text = result;
                trw.Cells.Add(cel);

                //是否合格
                cel = new TableCell();
                cel.Text = Pass;
                cel.ForeColor = Pass.Equals("合格") ? Color.Green : Color.Red;
                trw.Cells.Add(cel);
            }
            else
            {
                cel = new TableCell();
                cel.Text = "未录入";
                cel.ForeColor = Color.Red;
                cel.ColumnSpan = 2;
                trw.Cells.Add(cel);
            }
           
           

            tb.Rows.Add(trw);
            trw = new TableRow();
            count += 1;
        }
        return count;
    }
}
